package com.boolib.aop.config;

import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Slf4j
@Component
public class MethodBeforeAop  implements MethodInterceptor {


//    @Override
//    public void before(Method method, Object[] objects, Object o) throws Throwable {
//
//        String className = o.getClass().getName();
//        String methodName = method.getName();
//        System.out.println("className = " + className);
//        System.out.println("methodName = " + methodName);
//    }

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

        String name = methodInvocation.getMethod().getName();

        long time = System.currentTimeMillis();
        Object proceed = methodInvocation.proceed();

        log.info("class:{},method:{},cost:{}ms",methodInvocation.getThis().getClass().getName(),name,(System.currentTimeMillis()-time));


        return proceed;
    }
}
